package com.ios.controller;

import com.ios.dto.AttentionExecution;
import com.ios.entity.Attention;
import com.ios.enums.AttentionStateEnum;
import com.ios.service.IAttentionService;
import com.ios.service.impl.AttentionServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/attention",method = {RequestMethod.POST})
public class AttentionController {
    @Autowired
    private IAttentionService attentionService;

    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> Attention(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Attention attention = new Attention();
        /*取值并放入对象*/
        Integer disablerId1 = HttpServletRequestUtil.getInt(request,"disabler_id1");
        Integer disablerId2 = HttpServletRequestUtil.getInt(request,"disabler_id2");
        if(disablerId1!=-1&&disablerId2!=-1){
            AttentionExecution ae = attentionService.AddAttention(disablerId1,disablerId2);
            if (ae.getState()== AttentionStateEnum.A_SUCCESSS.getState()){
                modelMap.put("success",true);
                modelMap.put("attentionInfo",ae.getAttention());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",ae.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请给出完整残疾人用户id");
        }
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> DeleteAttention(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Attention attention = new Attention();
        /*取值并放入对象*/
        Integer disablerId1 = HttpServletRequestUtil.getInt(request,"disabler_id1");
        Integer disablerId2 = HttpServletRequestUtil.getInt(request,"disabler_id2");
        if(disablerId1!=-1&&disablerId2!=-1){
            AttentionExecution ae = attentionService.DeleteAttention(disablerId1,disablerId2);
            if (ae.getState()== AttentionStateEnum.UA_SUCCESS.getState()){
                modelMap.put("success",true);
                modelMap.put("Msg","取关成功");
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",ae.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请给出完整残疾人用户id");
        }
        return modelMap;
    }

}

